package cn.trigram.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class EchoController {

  @GetMapping(value = "/echo/{string}")
  public String echo(
      @PathVariable
          String string
  ) {

    return "This is Nacos Service02.Your sent message is: " + string;
  }

  @GetMapping(value = "/error-echo/{string}")
  @SentinelResource(blockHandler = "errorEchoBlockHandler", fallback = "errorEchoFallback")
  public String errorEcho(
      @PathVariable
          String string
  ) {

    throw new IllegalArgumentException("错误回显:" + string);
  }

  public String errorEchoBlockHandler(String string, BlockException t) {

    log.error(t.getClass().getName() + ":" + t.getMessage());
    log.error("service02的/error-echo/{string} 降级");
    return "service02的/error-echo/{string} 降级响应";
  }

  public String errorEchoFallback(String string, Throwable t) {

    log.error(t.getClass().getName() + ":" + t.getMessage());
    log.error("service02的/error-echo/{string} 熔断");
    return "service02的/error-echo/{string} 熔断响应";
  }

}
